<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    function index(){
    	$t_number = session('t_number');
    	if ($t_number == false) {
    		$this->redirect('Manager/login');
    	} else {
    		$student = D('Student')->where('t_number=' . $t_number)->find();
    		unset($student['password']);
    		$this->assign('student',$student);
    		 
    		$sql = 'SELECT * FROM ts_question where type=1 ORDER BY rand() LIMIT 20';
    		$que1 = D('Question')->query($sql);
    		$this->assign('que1',$que1);
    		
    		$sql = 'SELECT * FROM ts_question where type=2 ORDER BY rand() LIMIT 10';
    		$que2 = D('Question')->query($sql);
    		$this->assign('que2',$que2);
    		
    		$sql = 'SELECT * FROM ts_question where type=3 ORDER BY rand() LIMIT 10';
    		$que3 = D('Question')->query($sql);
    		$this->assign('que3',$que3);
    	
    		$sql = 'SELECT * FROM ts_question where type=4 ORDER BY rand() LIMIT 4';
    		$que4 = D('Question')->query($sql);
    		$this->assign('que4',$que4);
    		
    		$sql = 'SELECT * FROM ts_question where type=5 ORDER BY rand() LIMIT 2';
    		$que5 = D('Question')->query($sql);
    		$this->assign('que5',$que5);
    		
    		$sql = 'SELECT * FROM ts_question where type=6 ORDER BY rand() LIMIT 1';
    		$que6 = D('Question')->query($sql);
    		$this->assign('que6',$que6);
    		
    		if (!empty($_POST)) {
    			$v = array();
    			$a = array();
    			$an = array();
    			foreach ($_POST as $k=>$p) {
    				if (is_array($p)) {
    					$p = implode(',', $p);
    				}
    				$z = D('Question')->where('id=' . $k)->find();
    				$zz = fopen($z['answer'], 'r');
    				$r = fread($zz,filesize($z['answer']));
    				
    				if ($z['type'] != 5 and $z['type'] != 6) {
    					if ($r === '{literal}' . $p . '{/literal}') {
    						$v[] = $k;
    						array_merge($v);
    					}
    				} else {
    					$an[] = $k;
    					array_merge($an);
    					$repet = D('Mark')->where('st_id=' . $t_number)->find();
    					if (!$repet) {
    						D('Mark')->st_id = $t_number;
    						$new = D('Mark')->add();
    					}
    					$qu_id = implode(',', $an);
    					
    					$a[] = $p;
    					array_merge($a);
    					
    					$anser = implode('<test/>', $a);
    					$question = new \Model\QuestionModel();
    					$file = $question->writefile('Upload/sanswer/', $anser);
    				}
    				if ($qu_id != false) {
    					$zz = "update ts_mark set qu_id='$qu_id' where st_id='$t_number'";
    					
    					$s_Id = M('Mark')->execute($zz);
    					if ($s_Id) {
    						$save = "update ts_mark set answer='$file' where st_id='$t_number'";
    						$s_an = M('Mark')->execute($save);
    					}
    				}
    				
    				$q = implode(',', $v);
    				if ($q != '') {
	    				$m1 = D('Question')->where('id in(' . $q . ') and type=1' )->count();
	    				$m2 = D('Question')->where('id in(' . $q . ') and type=2' )->count();
	    				$m3 = D('Question')->where('id in(' . $q . ') and type=3' )->count();
	    				$m4 = D('Question')->where('id in(' . $q . ') and type=4' )->count();
	    				 
	    				$mark = $m1*1+$m2*2+$m3*2+$m4*5; 
    				}
    				fclose($zz);
    			}
    			if ($mark === 0) {
    				$this->redirect('Tips/mark');
    			} else {
    				D('Student')->mark = $mark;
    				$mm = D('Student')->where('t_number=' . $t_number)->save();
    				if ($mm) {
    					$this->redirect('Tips/mark');
    				}
    			}
    		}
    	}
    	$this->display();
    }
}